package com.ruoyi.park.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.park.domain.WyMaterialManagement;
import com.ruoyi.park.mapper.WyMaterialManagementMapper;
import com.ruoyi.park.service.IWyMaterialManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 物资管理Service业务层处理
 *
 * @author cjie
 * @date 2021-07-22
 */
@Service
public class WyMaterialManagementServiceImpl implements IWyMaterialManagementService {
    @Autowired
    private WyMaterialManagementMapper wyMaterialManagementMapper;

    /**
     * 查询物资管理
     *
     * @param id 物资管理ID
     * @return 物资管理
     */
    @Override
    public WyMaterialManagement selectWyMaterialManagementById(String id) {
        return wyMaterialManagementMapper.selectWyMaterialManagementById(id);
    }

    /**
     * 查询物资管理列表
     *
     * @param wyMaterialManagement 物资管理
     * @return 物资管理
     */
    @Override
    public List<WyMaterialManagement> selectWyMaterialManagementList(WyMaterialManagement wyMaterialManagement) {
        return wyMaterialManagementMapper.selectWyMaterialManagementList(wyMaterialManagement);
    }

    /**
     * 新增物资管理
     *
     * @param wyMaterialManagement 物资管理
     * @return 结果
     */
    @Override
    public int insertWyMaterialManagement(WyMaterialManagement wyMaterialManagement) {
        wyMaterialManagement.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        wyMaterialManagement.setCreateTime(DateUtils.getNowDate());
        wyMaterialManagement.setCreateBy(SecurityUtils.getUsername());

        Long amount = wyMaterialManagement.getAmount();
        BigDecimal unitPrice = wyMaterialManagement.getUnitPrice();
        if(StringUtils.isNotNull(amount) && StringUtils.isNotNull(unitPrice)){
            BigDecimal amountDe = new BigDecimal(amount);
            wyMaterialManagement.setTotalPrice(amountDe.multiply(unitPrice));
        }
        return wyMaterialManagementMapper.insertWyMaterialManagement(wyMaterialManagement);
    }

    /**
     * 修改物资管理
     *
     * @param wyMaterialManagement 物资管理
     * @return 结果
     */
    @Override
    public int updateWyMaterialManagement(WyMaterialManagement wyMaterialManagement) {
        wyMaterialManagement.setUpdateTime(DateUtils.getNowDate());
        wyMaterialManagement.setUpdateBy(SecurityUtils.getUsername());
        Long amount = wyMaterialManagement.getAmount();
        BigDecimal unitPrice = wyMaterialManagement.getUnitPrice();
        if(StringUtils.isNotNull(amount) && StringUtils.isNotNull(unitPrice)){
            BigDecimal amountDe = new BigDecimal(amount);
            wyMaterialManagement.setTotalPrice(amountDe.multiply(unitPrice));
        }
        return wyMaterialManagementMapper.updateWyMaterialManagement(wyMaterialManagement);
    }

    /**
     * 批量删除物资管理
     *
     * @param ids 需要删除的物资管理ID
     * @return 结果
     */
    @Override
    public int deleteWyMaterialManagementByIds(String[] ids) {
        return wyMaterialManagementMapper.deleteWyMaterialManagementByIds(ids);
    }

    /**
     * 删除物资管理信息
     *
     * @param id 物资管理ID
     * @return 结果
     */
    @Override
    public int deleteWyMaterialManagementById(String id) {
        return wyMaterialManagementMapper.deleteWyMaterialManagementById(id);
    }
}
